package com.cy.pj.sys.service.impl;

import com.cy.pj.common.annotation.RequiredLog;
import com.cy.pj.common.pojo.CheckBox;
import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.common.utils.AssertUtils;
import com.cy.pj.sys.dao.SysRoleDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.pojo.SysRole;
import com.cy.pj.sys.pojo.SysRoleMenu;
import com.cy.pj.sys.service.SysRoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
   @Autowired
   private SysRoleMenuDao sysRoleMenuDao;

    @RequiredLog
    @Override
    public PageObject<SysRole> findPageObjects(String name, Long pageCurrent) {
        AssertUtils.isValid(pageCurrent==null || pageCurrent<1, "当前页码值无效");
        int pageSize=2;
        //设置查询位置以及页面大小
        Page<SysRole> page= PageHelper.startPage(pageCurrent.intValue(),pageSize);
        //查询当前页数据
        List<SysRole> records= sysRoleDao.findPageObjects(name);
        return new PageObject<>(page.getTotal(),records,pageSize,pageCurrent);
       // return new PageObject<>(page.getTotal(),records,Long.valueOf(page.getPages()),pageSize,pageCurrent);
    }
    @RequiredLog
    @RequiresPermissions("sys:role:delete")
    @Override
    public int deleteObject(Integer id) {

        AssertUtils.isValid(id==0||id<0, "请先选择");
        sysRoleMenuDao.deleteObjectsByMenuId(id);
        sysUserRoleDao.deleteObjectsByRoleId(id);
        int rows=sysRoleDao.deleteObject(id);

        AssertUtils.isServiceValid(rows==0,"记录可能已经不存在");
        return rows;
    }

    @RequiredLog
    @RequiresPermissions("sys:role:add")
    @Override
    public int saveObject(SysRole sysRole, Integer[] menuIds) {
        //1.参数校验

        AssertUtils.isValid(sysRole==null,"保存对象不能为空");
        AssertUtils.isValid(StringUtils.isEmpty(sysRole.getName()),"角色名不能为空");
        AssertUtils.isValid(menuIds==null||menuIds.length==0,"必须为角色分配权限");

        //2.保存角色自身信息
        int rows=sysRoleDao.insertObject(sysRole);
        //3.保存角色菜单关系数据
        sysRoleMenuDao.insertObjects(sysRole.getId(), menuIds);
        return rows;
    }
    @RequiredLog
    @RequiresPermissions("sys:role:view")
    @Override
    public SysRoleMenu findObjectById(Integer id) {

        AssertUtils.isValid(id==null||id<=0, "id的值不合法");
        SysRoleMenu result=sysRoleDao.findObjectById(id);

        AssertUtils.isServiceValid(result==null,"此记录已经不存在");
        return result;
    }
    @RequiredLog
    @RequiresPermissions("sys:role:update")
    @Override
    public int updateObject(SysRole sysRole, Integer[] menuIds) {
        AssertUtils.isValid(sysRole==null,"更新对象不能为空");
        AssertUtils.isValid(sysRole.getId()==null,"ID值不能为空");
        AssertUtils.isValid(StringUtils.isEmpty(sysRole.getName()),"角色名字不能为空");
        AssertUtils.isValid(menuIds==null || menuIds.length==0,"必须为角色指定一个权限");
        int rows=sysRoleDao.updateObject(sysRole);

        AssertUtils.isServiceValid(rows==0,"对象可能已经不存在");
        sysRoleMenuDao.deleteObjectsByMenuId(sysRole.getId());
        sysRoleMenuDao.insertObjects(sysRole.getId(), menuIds);


        return rows;
    }

    @Override
    public List<CheckBox> findObjects() {

        return sysRoleDao.findObjects();
    }
}
